#include "agreeend.h"
#include "ui_agreeend.h"
#include "mainwindow.h"
#include <vector>
#include <QString>
#include "root.h"
#include <QMessageBox>
#include "program.h"
#include <QDebug>
#include "bigsave.h"
#include "teacher.h"
#include "student.h"
extern vector<Program*> prdata;
extern vector<Teacher*> tdata;
extern vector<student*> sdata;
int beg=-1;
int en=-1;
int now=-1;
agreeend::agreeend(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::agreeend)
{

    ui->setupUi(this);
    for(unsigned int i=0;i<prdata.size();i++)
    {
        if(prdata[i]->agreeornot())
         {   ui->lineEdit->setText(prdata[i]->showid());
            beg=i;
            now =i;
        break;}
    }
    for(unsigned int i=prdata.size()-1;i>=0;i--){///寻找最后一个
        if(prdata[i]->agreeornot()){
            en =i;
            break;}
    }
}

agreeend::~agreeend()
{
    delete ui;
}

void agreeend::on_pushButton_clicked()
{   if(ui->radioButton->isChecked()==0&&ui->radioButton_2->isChecked()==0){
    QMessageBox::information(this,"提示","请选择同意或不同意按钮");}
    else if(ui->radioButton->isChecked()){ ///选中了同意
            prdata[now]->agree(0);
            prdata[now]->setstatus(1);
            for(unsigned int i=0;i<tdata.size();i++){
                if(prdata[now]->ifhere(tdata[i]->findname())){
                    tdata[i]->addprogram();
                }
            }
            for(unsigned int i=0;i<sdata.size();i++){
                if(prdata[now]->ifhere(sdata[i]->findname())){
                    sdata[i]->addprogram();
                }
            }
            this->hide();
            bigsave();
            QMessageBox::information(this,"提示","同意结题成功");
            if(beg==en){
            root *p = new root;
            p->show();}
            else{
                agreeend *p = new agreeend;
                p->show();
            }
        }
    else {///选中的是不同意
            prdata[now]->agree(0);
            prdata[now]->setstatus(0);
             this->hide();
            bigsave();
            QMessageBox::information(this,"提示","驳回结题申请成功");
            if(beg==en){
            root *p = new root;
            p->show();}
            else{
                agreeend *p = new agreeend;
                p->show();
            }
        }
}

void agreeend::on_pushButton_2_clicked()
{
    this->close();
    root *p= new root;
    p->show();
}

void agreeend::on_pushButton_3_clicked()
{
        if(now==beg||now==0){
            QMessageBox::information(this,"提示","这已经是第一个");
        }else{

        for(int i=now-1;i>=beg;i--){
            if(prdata[i]->agreeornot()){
                now=i;
            ui->lineEdit->setText(prdata[now]->showid());
            break;
        }}}
}

void agreeend::on_pushButton_4_clicked()
{
        if(now==en||now==prdata.size()-1){
            QMessageBox::information(this,"提示","这已经是最后一个");
        }else {
            for(int i=now+1;i<=en;i++){
                if(prdata[i]->agreeornot()){
                    now=i;
                ui->lineEdit->setText(prdata[now]->showid());
                break;}
            }
        }
}


